﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ModelPredictPro.General.IO;
using System.Diagnostics;
using ModelPredictPro.Predict.SVM;

namespace ModelPredictPro.WindowForms.UserControls {
    public partial class UCNFold : SVMUserControl {
        public UCNFold() {
            InitializeComponent();
        }

        string PositiveSet {
            get { return txtPositiveDatasetFile.Text; }
            set { txtPositiveDatasetFile.Text = value; }
        }
        string NegativeSet {
            get { return txtNegativeDatasetFile.Text; }
            set { txtNegativeDatasetFile.Text = value; }
        }
        int NFold {
            get { return (int)numCacheMemorySize.Value; }
            set { numCacheMemorySize.Value = (decimal)value; }
        }

        private void btnNFold_Click(object sender, EventArgs e) {
            bool validate = true;
            validate = txtPositiveDatasetFile.CheckFileExist();
            //validate = txtNegativeDatasetFile.CheckFileExist();
            validate = txtFileNameTemplate.CheckInputPattern(@"\\.*\{1\}", "Please input file name template: {0} for original directory, {1} for number in nfold.  e.g. C:\\{1} for C:\\1.train C:\\2.train ......");
            if (!validate)
                return;
            string[] trains, tests;
            SetFileNames(NFold,  PositiveSet.GetDirectory(), txtFileNameTemplate.Text,  out trains, out tests);
            FileHelper.GenerateNFoldFiles(PositiveSet, NegativeSet, NFold, ref trains, ref tests);
            Process.Start("explorer.exe", trains[0].GetDirectory());
        }
        void SetFileNames(int nfold, string initDirectory, string pattern, out string[] trainFiles, out string[] testFiles) {
            trainFiles = new string[nfold];
            testFiles = new string[nfold];
            for (int i = 0; i < nfold; i++) {
                trainFiles[i] = string.Concat(string.Format(pattern, initDirectory, i + 1), ".train");
                testFiles[i] = string.Concat(string.Format(pattern, initDirectory, i + 1), ".test");
            }
        }

        private void btnRunSVM_Click(object sender, EventArgs e) {
            bool validate = true;
            validate = txtPositiveDatasetFile.CheckFileExist() && validate;
            validate = txtNegativeDatasetFile.CheckFileExist() && validate;
            validate = txtFileNameTemplate.CheckInputPattern(@"\\.*\{1\}", "Please input file name template: {0} for original directory, {1} for number in nfold.  e.g. C:\\{1} for C:\\1.train C:\\2.train ......") && validate;
            validate = txtSVMFileTemplate.CheckInputPattern(@"\\.*\{1\}", "Please input file name template: {0} for original directory, {1} for number in nfold.  e.g. C:\\{1} for C:\\1.train C:\\2.train ......") && validate;
            if (!validate)
                return;
            string[] trains, tests;
            string initDirecotry = PositiveSet.GetDirectory();
            SetFileNames(NFold, initDirecotry , txtFileNameTemplate.Text,  out trains, out tests);
            FileHelper.GenerateNFoldFiles(PositiveSet, NegativeSet, NFold, ref trains, ref tests);
            Process.Start("explorer.exe", trains[0].GetDirectory());


            SVMFactory factory = UCSVMSetting.SingleInstance.CreateSVMFactory();
            var parma = factory.CreatePredictParam();
            string svmFilePattern = txtSVMFileTemplate.Text;
            for (int i = 0; i < NFold; i++) {
                string train = trains[i];
                string test = tests[i];
                string paramOutfile = string.Concat(string.Format(svmFilePattern, initDirecotry, i + 1), ".param");
                string resultOutfile = string.Concat(string.Format(svmFilePattern, initDirecotry, i + 1), ".result");
                ITask task = new TrainPredictTask(parma, train, test, paramOutfile, resultOutfile);
                AddTask(task);
            }
            if (TaskListForm.Visible == false)
                TaskListForm.Show();
            MessageBox.Show(string.Format("Add {0} tasks, click RUN to continue.", NFold ));

        }

    }
}
